package view;

import java.util.ArrayList;
import java.util.List;

import model.Contract;
import model.Lead;
import model.Opportunity;
import model.SMSObject;
import model.User;
import util.SMSUtil;

public abstract class View {

	protected List leadObjects = new ArrayList();

	protected List contractObjects = new ArrayList();

	protected List oppObjects = new ArrayList();

	protected User user = null;

	public View(User user) {
		this.user = user;

	}

	public void processView() {
		try {
			retrieveView();
			displayView();
		} catch (NumberFormatException ne) {
			System.out.println("Incorrect format!");
		}
	}

	abstract void retrieveView();

	public void displayView() {

		if (!user.getRole().equalsIgnoreCase("administrator")) {
			SMSUtil.displayEnd();
			if (leadObjects.size() != 0) {
				System.out
						.println("\t\t\t\t\t********************Leads List*************************");
				SMSUtil.displayLeadHeader();
				for (Object o : leadObjects) {
					SMSUtil.formatLead((Lead) o);
				}

			} else {
				System.out
						.println("\t\t\t\t\t********************No lead objects to be processed*************************");

			}
			SMSUtil.displayEnd();
			if (oppObjects.size() != 0) {
				System.out
						.println("\t\t\t\t\t********************Opportunities List*************************");
				SMSUtil.displayOpportunityHeader();
				for (Object o : oppObjects) {
					SMSUtil.formatOpportunity((Opportunity) o);
				}

			} else {
				System.out
						.println("\t\t\t\t\t********************No Opportunity objects to be processed*************************");

			}
			SMSUtil.displayEnd();
			if (contractObjects.size() != 0) {
				System.out
						.println("\t\t\t\t\t********************Contracts List*************************");
				SMSUtil.displayContractHeader();
				for (Object o : contractObjects) {
					SMSUtil.formatContract((Contract) o);
					//System.out.println((Contract) o);
				}

			} else {
				System.out
						.println("\t\t\t\t\t********************No Contract objects to be processed*************************");

			}
			SMSUtil.displayEnd();
			if (oppObjects.size() != 0 || contractObjects.size() != 0
					|| leadObjects.size() != 0) {
				SMSObject smsobj = null;
				
					System.out
							.println("Please select an SMSObject ID to process :");
					String choice = SMSUtil.getUserInput();
					smsobj = (SMSObject) retrieveObject(Long
							.parseLong(choice));
					
					if(smsobj == null){
						System.out.println("Invalid Id. Please select from the above list of IDs only!!!");
					}else{
						smsobj.getCurrentState().execute(smsobj);
					}
			}
		} 

	}

	public Object retrieveObject(long id) {

		if (!user.getRole().equalsIgnoreCase("Administrator")) {
			for (Object o : leadObjects) {
				SMSObject smsObj = (SMSObject) o;
				if (smsObj.getSmsObjId() == id) {
					return smsObj;
				}
			}
			for (Object o : oppObjects) {
				SMSObject smsObj = (SMSObject) o;
				if (smsObj.getSmsObjId() == id) {
					return smsObj;
				}
			}
			for (Object o : contractObjects) {
				SMSObject smsObj = (SMSObject) o;
				if (smsObj.getSmsObjId() == id) {
					return smsObj;
				}
			}
		}
		return null;
	}

	protected boolean checkUser(SMSObject smsObj) {
		if (smsObj.getManagedByUser().getId() == user.getId()) {
			return true;
		}
		return false;
	}

	protected List getSMSObjectForUser(List list) {

		List newList = new ArrayList();
		if (list.size() != 0) {

			for (Object o : list) {
				SMSObject obj = (SMSObject) o;
				if (checkUser(obj)) {
					newList.add(obj);
				}
			}

		}

		return newList;
	}

}
